#include <func.h>

void print_wstatus(int status) 
{
    if (WIFEXITED(status)) {
        // 子进程正常终止
        printf("exit code: %d\n", WEXITSTATUS(status));
    } else if (WIFSIGNALED(status)) {
        printf("terminate signal: %d", WTERMSIG(status));
#ifdef WCOREDUMP 
        if (WCOREDUMP(status)) {
            printf(" (core dump)\n");
        }
#endif
    } else if (WIFSTOPPED(status)) {
        printf("stop signal: %d\n", WSTOPSIG(status));
    } else if (WIFCONTINUED(status)) {
        printf("continued\n");
    }
}

int main(int argc, char* argv[])
{
    int pid = fork();
    switch (pid) {
    case -1:
        perror("fork()");
        exit(1);
    case 0:     // 子进程
        for(;;) ;
        /* exit(67); */
        /* abort(); */
    default:    // 父进程
        int status;
        /* pid_t child = waitpid(-1, &status, 0);   /1* 等价于 wait *1/ */
        /* pid_t child = waitpid(-1, &status, WNOHANG); */  
        /* pid_t child = waitpid(-1, &status, 0);   */
        /* pid_t child = waitpid(-1, &status, WUNTRACED);   /1* 等价于 wait *1/ */
        pid_t child = waitpid(-1, &status, WCONTINUED);   
        if (child == -1) {
            perror("waitpid()");
            exit(1);
        } else if (child == 0) {
            printf("No child change status\n");
        } else {
            printf("Child %d change status: ", child);
            print_wstatus(status);
        }
        break;
    }
    return 0;
}

